package com.panshi.controller;

import com.panshi.service.IQuestionService;
import com.panshi.vo.qa.DataResultQA;
import com.panshi.vo.qa.Pages;
import com.panshi.vo.qa.Question;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Api(tags = "问题业务接口")
@Controller
@RequestMapping(value = "/question", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Slf4j
public class QuestionController {

    @Autowired
    IQuestionService questionService;

   /* @RequestMapping(value = "/add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @ApiOperation("问题添加")
    @ApiImplicitParam(name = "question",value = "问题信息")
    public boolean addQuestion(Question question){
        log.info("请求路径：{},参数：{}","/question/add",question);
        boolean boo = questionService.addQuestion(question);
        return boo;
    }*/

    @PostMapping(value = "/inquirePages")
    @ResponseBody
    @ApiOperation("问题分页查询")
    @ApiImplicitParam(name = "pages",value = "分页查询条件",required = true)
    public DataResultQA<Pages<Question>> inquirePagesQuestion(@RequestBody Pages pages){
        log.info("请求路径：{},参数：{}","/question/inquirePages",pages);
        DataResultQA<Pages<Question>> dataResultQA = new DataResultQA();
        if(pages.getStartPage() < 0 || pages.getPageSize() <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }

        Pages<Question> p = questionService.inquirePagesQuestion(pages);

        log.info("请求结果：{}",p);

        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("分页查询完成！");
        dataResultQA.setData(p);
        return dataResultQA;
    }

    @PostMapping("/inquireQuestionSum")
    @ResponseBody
    @ApiOperation("查询数据条数")
    @ApiImplicitParam(name = "pages",value = "查询条件",required=true)
    public DataResultQA<Integer> inquireQuestionSum(@RequestBody Pages pages){
        log.info("请求路径：{},参数：{}","/question/inquireQuestionSum",pages);
        DataResultQA<Integer> dataResultQA = new DataResultQA();

        if(pages.getStartPage() < 0 || pages.getPageSize() <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        Integer sum= questionService.inquireQuestionSum(pages);
        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("查询数据条数！");
        dataResultQA.setData(sum);
        return dataResultQA;
    }

    @PostMapping(value = "/inquireById/{id}")
    @ResponseBody
    @ApiOperation("根据问题id查询")
    @ApiImplicitParam(name = "id",value = "问题id",required = true)
    public DataResultQA<Question> inquireById(@PathVariable int id){
        log.info("请求路径：{},参数：{}","/question/inquireById/{id}",id);
        DataResultQA<Question> dataResultQA = new DataResultQA<>();
        if(id <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        Question question = questionService.inquireById(id);
        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("根据id查询！");
        dataResultQA.setData(question);
        return dataResultQA;
    }

    /*//阅读总数加一
    @GetMapping("/readSumAnd1/{id}")
    @ResponseBody
    @ApiOperation("阅读总数加一")
    @ApiImplicitParam(name = "id",value = "问题id",required = true)
    public boolean readSumAnd1(int id){
        log.info("请求路径：{},参数：{}","/question/readSumAnd1/{id}",id);
        boolean boo = questionService.readSumAnd1(id);
        return boo;
    }*/

    //收藏总数加一
    @GetMapping(value = "/collectSumAnd1/{id}")
    @ResponseBody
    @ApiOperation("收藏总数加一")
    @ApiImplicitParam(name = "id",value = "问题id",required = true)
    public DataResultQA collectSumAnd1(@PathVariable int id){
        log.info("请求路径：{},参数：{}","/question/collectSumAnd1/{id}",id);
        DataResultQA dataResultQA = new DataResultQA();
        if(id <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        boolean boo = questionService.collectSumAnd1(id);
        if(boo){
            dataResultQA.setCode(200);
            dataResultQA.setBoo(true);
            dataResultQA.setDescribe("收藏总数加一完成！");
        }else{
            dataResultQA.setCode(120);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("收藏总数加一失败！");
        }
        return dataResultQA;
    }

    //收藏总数减一
    @GetMapping(value = "/collectSumSubtract1/{id}")
    @ResponseBody
    @ApiOperation("收藏总数减一")
    @ApiImplicitParam(name = "id",value = "问题id",required = true)
    public DataResultQA collectSumSubtract1(@PathVariable int id){
        log.info("请求路径：{},参数：{}","/question/collectSumSubtract1/{id}",id);
        DataResultQA dataResultQA = new DataResultQA();
        if(id <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        boolean boo = questionService.collectSumSubtract1(id);
        if(boo){
            dataResultQA.setCode(200);
            dataResultQA.setBoo(true);
            dataResultQA.setDescribe("收藏总数减一完成！");
        }else{
            dataResultQA.setCode(120);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("收藏总数减一失败！");
        }
        return dataResultQA;
    }

    /*//回答总数加一
    @GetMapping("/commentSumAnd1/{id}")
    @ResponseBody
    @ApiOperation("回答总数加一")
    @ApiImplicitParam(name = "id",value = "问题id",required = true)
    public boolean commentSumAnd1(int id){
        log.info("请求路径：{},参数：{}","/question/commentSumAnd1/{id}",id);
        boolean boo = questionService.commentSumAnd1(id);
        return boo;
    }*/

    //回答总数减一
    @GetMapping("/commentSumSubtract1/{id}")
    @ResponseBody
    @ApiOperation("回答总数减一")
    @ApiImplicitParam(name = "id",value = "问题id",required = true)
    public DataResultQA commentSumSubtract1(@PathVariable int id){
        log.info("请求路径：{},参数：{}","/question/commentSumSubtract1/{id}",id);
        DataResultQA dataResultQA = new DataResultQA();
        if(id <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        boolean boo = questionService.commentSumSubtract1(id);
        if(boo){
            dataResultQA.setCode(200);
            dataResultQA.setBoo(true);
            dataResultQA.setDescribe("回答总数减一完成！");
        }else{
            dataResultQA.setCode(120);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("回答总数减一失败！");
        }
        return dataResultQA;
    }

    @GetMapping(value = "/delete/{id}")
    @ResponseBody
    @ApiOperation("问题删除（标记删除）")
    @ApiImplicitParam(name = "id",value = "问题id",required = true)
    public DataResultQA deleteQuestion(@PathVariable int id){
        log.info("请求路径：{},参数：{}","/question/delete/{id}",id);
        DataResultQA dataResultQA = new DataResultQA();
        if(id <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        boolean boo = questionService.deleteQuestion(id);
        if(boo){
            dataResultQA.setCode(200);
            dataResultQA.setBoo(true);
            dataResultQA.setDescribe("标记删除完成！");
        }else{
            dataResultQA.setCode(120);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("标记删除失败！");
        }
        return dataResultQA;
    }

    //用于用户查询自己发布的问题(分页查询)
    @PostMapping(value = "/inquireMyPagesQuestion")
    @ResponseBody
    @ApiOperation("用户查询自己发布的问题")
    @ApiImplicitParam(name = "pages",value = "分页查询条件")
    public DataResultQA<Pages<Question>> inquireMyPagesQuestion(@RequestBody Pages pages){
        log.info("请求路径：{},参数：{}","/question/inquireMyPagesQuestion",pages);
        DataResultQA<Pages<Question>> dataResultQA = new DataResultQA();
        if(pages.getStartPage() < 0 || pages.getPageSize() <= 0 ||pages.getUserId() <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        Pages<Question> p = questionService.inquireMyPagesQuestion(pages);
        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("根据用户id分页查询完成！");
        dataResultQA.setData(p);
        return dataResultQA;
    }

}
